package cn.gavin.web.controller;

import cn.gavin.domain.SystemDictionaryItem;
import cn.gavin.qo.JsonResult;
import cn.gavin.qo.QueryObject;
import cn.gavin.qo.SystemDictionaryItemQueryObject;
import cn.gavin.service.ISystemDictionaryItemService;
import cn.gavin.service.ISystemDictionaryService;
import cn.gavin.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequiredPermission(name = "字典明细页面",expression = "systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        model.addAttribute("dictionaries",systemDictionaryService.listAll());
        model.addAttribute("pageInfo",systemDictionaryItemService.query(qo));
        return "systemDictionaryItem/list";
    }

    @RequiredPermission(name = "字典明细删除",expression = "systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            systemDictionaryItemService.deleteById(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }

/*
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.findById(id);
            model.addAttribute("systemDictionaryItem",systemDictionaryItem);
        }
        return "systemDictionaryItem/input";
    }
*/

    @RequiredPermission(name = "字典明细新增/编辑",expression = "systemDictionaryItem:save")
    @RequestMapping("/save")
    @ResponseBody // @ResponseBody的作用其实是将java对象转为json格式的数据
    public JsonResult save(SystemDictionaryItem systemDictionaryItem){
        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        }else {
            systemDictionaryItemService.insert(systemDictionaryItem);
        }
        return new JsonResult();
    }
}
